package com.yozinga.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.Log;

import com.yozinga.fragment.HomePageFragment.EvalReceiver;

public class AddressConversion {

	String mAddress;
	Context mContext;
	EvalReceiver mEvalReceiver;

	public AddressConversion(String address,Context context,EvalReceiver evalReceiver) {
		mAddress = address;
		mContext = context;
		mEvalReceiver = evalReceiver;
		new Task().execute();
	}

	class Task extends AsyncTask<String, Void, LocationValues>{  

		@Override
		protected LocationValues doInBackground(String... arg0) {
			String addressString = null;
			try {
				if(mAddress.contains(" ")){
					mAddress = mAddress.replace(" ",",");
				}
				addressString = URLEncoder.encode(mAddress,"UTF-8");
			}catch (UnsupportedEncodingException unsupportedEncodingException){
				Log.i("Today", "UnsupportedEncodingException Exception is"+unsupportedEncodingException.getMessage());
			}
			
			LocationValues locationValues = getLocationInfo(addressString);
			
			Log.i("NCS", "Latitude is  " + locationValues.latitude);
			Log.i("NCS", "Longitude  is" + locationValues.longitude);
			return locationValues;
		}

		@Override
		protected void onPostExecute(LocationValues locationValues) {
			super.onPostExecute(locationValues);
			LocationValues mLocationValues = locationValues;
			ResultReceiver receiver = mEvalReceiver;
			Bundle locationData = new Bundle();
			if(mLocationValues.latitude!=null&& mLocationValues.longitude!=null){
				locationData.putDouble("latitude", mLocationValues.latitude);
				locationData.putDouble("longitude", mLocationValues.longitude);
				if (receiver != null) {
					receiver.send(2, locationData);
				}
			}else{
				locationData.putString("address", "Not Valid Address");
				if (receiver != null) {
					receiver.send(2, locationData);
				}
			}
			
		}

	}
	public static LocationValues getLocationInfo(String address) {

		HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +address+"&ka&sensor=false");
		HttpClient client = new DefaultHttpClient();
		HttpResponse response;
		StringBuilder stringBuilder = new StringBuilder();
		LocationValues locationValues = null;
		try {
			response = client.execute(httpGet);
			HttpEntity entity = response.getEntity();
			InputStream stream = entity.getContent();
			int b;
			while ((b = stream.read()) != -1) {
				stringBuilder.append((char) b);
			}
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}

		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject = new JSONObject(stringBuilder.toString());
			locationValues = getGeoPoint(jsonObject);
		} catch (JSONException e) {
			Log.i("NCS", "Exception 1 is  :  " + e.getMessage());
		}

		return locationValues;
	}

	public static LocationValues getGeoPoint(JSONObject jsonObject) {

		Double longitude = new Double(0);
		Double latitude = new Double(0);
		LocationValues locationValues = new LocationValues();
		try {
			longitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
					.getJSONObject("geometry").getJSONObject("location")
					.getDouble("lng");

			latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
					.getJSONObject("geometry").getJSONObject("location")
					.getDouble("lat");

			Log.i("NCS", "Latitude  " + longitude);
			Log.i("NCS", "Longitude  " + latitude);
			locationValues.latitude = latitude;
			locationValues.longitude = longitude;
		} catch (JSONException e) {
			Log.i("NCS", "Exception  2 is  :  " + e.getMessage());
		}
		return locationValues;
	}
}
